package com.hnl.action.user;

import java.io.IOException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hnl.core.HNLPageDispatch;
import com.hnl.core.aware.HNLAuthAware;
import com.hnl.core.aware.HNLTalentAware;
import com.hnl.core.aware.MiscTypeAware;
import com.hnl.dao.DAOException;
import com.hnl.dao.HNLDAOFactory;
import com.hnl.dao.UserDAO;
import com.hnl.to.User;

@SuppressWarnings("serial")
public class UpdateUserPassword extends HttpServlet implements HNLAuthAware,
		HNLTalentAware, MiscTypeAware, HNLPageDispatch {
	public static final Logger LOG = LoggerFactory.getLogger(UpdateUserPassword.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		try{
			Integer userID = Integer.valueOf(req.getParameter("userID"));
			String password = req.getParameter("password");
			HttpSession session = ((HttpServletRequest) req).getSession();			
			User userSession = (User) session.getAttribute(ATTRIBUTE_USER);
			
			UserDAO userDAO = HNLDAOFactory.getDAOFactory(
					HNLDAOFactory.HNLDAOFACTORY).getUserDAO();
			User newUser = userDAO.loadUserByUserID(Integer.valueOf(userID));
			Date now = new Date();
			
			newUser.setFirstname(newUser.getFirstname());
			newUser.setLastname(newUser.getLastname());
			newUser.setEmail(newUser.getEmail());
			newUser.setPassword(password);
			newUser.setLastLoginDate(newUser.getLastLoginDate());
			newUser.setIsDelete(newUser.getIsDelete());
			newUser.setCreateDate(newUser.getCreateDate());
			newUser.setCreateUserID(newUser.getCreateUserID());
			newUser.setModifyDate(now);
			newUser.setModifyUserID(userSession.getUserID());
			
			userDAO.updateUser(newUser);

			req.setAttribute("userID", userID);

			RequestDispatcher dispatcher = req
					.getRequestDispatcher(USER_INFO_PAGE);
			dispatcher.forward(req, resp);

		}catch (DAOException e) {
			LOG.error("Error to add activity!!", e);
		}

	}
	
	

}
